package com.iot.service;

import com.iot.mapper.DTmenuMapper;
import com.iot.mapper.DTroleMapper;
import com.iot.mapper.TroleMapper;
import com.iot.mapper.Trole_authorityMapper;
import com.iot.pojo.MTrole;
import com.iot.pojo.MTrole_authority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TroleServiceImpl extends BaseServiceImpl<MTrole> implements TroleService {
    @Autowired
    private TroleMapper mapper;
    @Autowired
    private DTroleMapper dmapper;
    @Autowired
    private DTmenuMapper dTmenuMapper;

    @Autowired
    private Trole_authorityMapper trole_authorityMapper;

    @Override
    public TroleMapper getMapper() {
        return mapper;
    }

    //    @Override
//    public int getRecordCount(String sWhere) {
//        return mapper.getRecordCount(sWhere);
//    }
//
    @Override
    public int insert(MTrole mTrole) throws Exception {
        int count = 0;
        count = getRecordCount("AND APP_ID =" + mTrole.getAPP_ID() + "  AND SNAME='" + mTrole.getSNAME() + "'");
        if (count > 0)
            throw new Exception("该名称已存在");
        return mapper.insert(mTrole);
    }

    @Override
    public void update(MTrole mTrole) throws Exception {
        int count = 0;
        count = getRecordCount("AND APP_ID =" + mTrole.getAPP_ID() + "  AND SNAME='" + mTrole.getSNAME() + "' AND ROLE_ID!=" + mTrole.getROLE_ID() + "");
        if (count > 0)
            throw new Exception("该名称已存在");
        mapper.update(mTrole);
    }
//
//    @Override
//    public void deleteById(int id) {
//        mapper.deleteById(id);
//    }
//
//    @Override
//    public MTrole single(Integer id) {
//        MTrole bean = mapper.single(id);
//        return bean;
//    }
//
//    @Override
//    public List<MTrole> fetch(String sWhere) {
//        return mapper.fetch(sWhere);
//    }

    @Override
    public List<Map<String, Object>> fetchByPage(Map<String, Object> map) {
//        Integer page = Common.toInt(map.get("page"), 1);
//        Integer size = Common.toInt(map.get("limit"), 10);
//
//        Integer start = (page - 1) * size;
//        map.put("start", start);
//        map.put("size", size);

        return dmapper.fetchByPage(map);

    }

    @Override
    public Integer countData(Map<String, Object> map) {
        return dmapper.countData(map);
    }

    @Override
    public List<Map<String, String>> getAuthorityByRoleIdList(int roleId) {
        return dTmenuMapper.getAuthorityByRoleIdList(roleId);
    }

    @Override
    public void saveAuthority(List<MTrole_authority> list) {
        dTmenuMapper.delAuthByRoldId(list.get(0).getROLE_ID());
        dTmenuMapper.insertAuth(list);
    }

    @Override
    public void deleteRole(int id) {
        dTmenuMapper.delAuthByRoldId(id);
        mapper.deleteById(id);
    }


}
